﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DAL;
using Cocbook_DTO;

namespace Sale_and_manage
{
    public partial class frmBookSearch : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmBookSearch()
        {
            InitializeComponent();
            if (Type == 1)
            {
                btnAddtoCash.Visible = true;
                LoaddgvBookSearch1();
            }
            else
            {
                labelX2.Visible = true;
                cboProjectName.Visible = true;
                LoadProject();
                LoaddgvBookSearch2();
            }
        }

        public static int Type;
        public Project project;
        public List<SBook2> lsb;
        public List<Project_Book> lpr_bo;
        public List<Project> lpr;

        public void LoadProject()
        {
            ProjectDAL prDAL = new ProjectDAL();
            lpr = prDAL.GetAllProject();
            foreach (Project pr in lpr)
            {
                cboProjectName.Items.Add(pr.ProjectName);
            }
        }

        private void btnAddtoCash_Click(object sender, EventArgs e)
        {
            if (dgvBookSearch.RowCount > 0 && dgvBookSearch.SelectedRows.Count > 0)
            {
                //load hàm frmAddOrder.LoadBook2() với ISBN, title và price nếu giá trị trả về true thì clear txtTitle
            }
            else
            {
                MessageBox.Show("Please select Book first");
            }
        }

        public int GetProjectIndex(Project p)
        {
            int result = 0;
            for (int i = 0; i < lpr.Count; i++)
            {
                if (lpr[i].ProjectID == p.ProjectID)
                {
                    result = i;
                    break;
                }
            }
            return result;
        }

        public void LoaddgvBookSearch2()
        {
            ProjectDAL p = new ProjectDAL();
            project = p.GetProjectByStatus();
            cboProjectName.SelectedIndex = GetProjectIndex(project);
            Project_BookDAL boDAL = new Project_BookDAL();
            lpr_bo = boDAL.GetProject_BookByProjectID(project.ProjectID);
            ConvertBook_SBook(lpr_bo);
            dgvBookSearch.DataSource = lsb;
            dgvBookSearch.Columns[0].Width = 60;
            dgvBookSearch.Columns[1].Width = 90;
            dgvBookSearch.Columns[2].Width = 500;
            dgvBookSearch.Columns[3].Width = 120;
            dgvBookSearch.Columns[4].Width = 70;
            dgvBookSearch.Columns[5].Width = 90;
        }

        public void LoaddgvBookSearch1()
        {
            ProjectDAL p = new ProjectDAL();
            Project_BookDAL boDAL = new Project_BookDAL();
            lpr_bo = boDAL.GetProject_BookByProjectID(1);
            ConvertBook_SBook(lpr_bo);
            dgvBookSearch.DataSource = lsb;
            dgvBookSearch.Columns[0].Width = 60;
            dgvBookSearch.Columns[1].Width = 90;
            dgvBookSearch.Columns[2].Width = 500;
            dgvBookSearch.Columns[3].Width = 120;
            dgvBookSearch.Columns[4].Width = 70;
            dgvBookSearch.Columns[5].Width = 90;
        }

        public void ConvertBook_SBook(List<Project_Book> lb)
        {
            lsb = new List<SBook2>();
            SBook2 sb = null;
            foreach (Project_Book b in lb)
            {
                sb = new SBook2(b);
                lsb.Add(sb);
            }

        }


        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((keyData == Keys.F1) && (Type == 1)) 
            {
                MessageBox.Show("asdas");
            }
            return base.ProcessDialogKey(keyData);
        }

        public void SearchBook1()
        {
            ProjectDAL p = new ProjectDAL();
            Project_BookDAL boDAL = new Project_BookDAL();
            lpr_bo = boDAL.GetAllProject_BookByProjectIDAndBookTitle(1,txtTitle.Text);
            ConvertBook_SBook(lpr_bo);
            dgvBookSearch.DataSource = lsb;            
        }

        public void SearchBook2()
        {
            ProjectDAL p = new ProjectDAL();
            Project_BookDAL boDAL = new Project_BookDAL();
            lpr_bo = boDAL.GetAllProject_BookByProjectIDAndBookTitle(project.ProjectID, txtTitle.Text);
            ConvertBook_SBook(lpr_bo);
            dgvBookSearch.DataSource = lsb;
        }

        private void txtTitle_TextChanged(object sender, EventArgs e)
        {
            if (txtTitle.Text == "")
            {
                if (Type == 1)
                {
                    LoaddgvBookSearch1();
                }
                else
                {
                    LoaddgvBookSearch2();
                }
            }
            else
            {
                if (Type == 1)
                {
                    SearchBook1();
                }
                else
                {
                    SearchBook2();
                }
            }
        }


        
    }
}